#include "stdafx.h"
#include "GrabDemo.h"
#include "DlgFeature.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define PAGESIZE        10

/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CDlgFeature 

CDlgFeature::CDlgFeature(CFGCamera *pCamera,CWnd* pParent /*=NULL*/)
	: CDialog(CDlgFeature::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgFeature)
	//}}AFX_DATA_INIT
        m_pCamera=pCamera;
}


void CDlgFeature::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgFeature)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgFeature, CDialog)
	//{{AFX_MSG_MAP(CDlgFeature)
	ON_WM_HSCROLL()
	ON_LBN_SELCHANGE(IDC_PARAMETER, OnSelchangeParameter)
	ON_BN_CLICKED(IDB_AUTO, OnFeatureAuto)
	ON_BN_CLICKED(IDB_ON, OnFeatureOn)
	ON_BN_CLICKED(IDB_ONESHOT, OnFeatureOneshot)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Init dialog.

BOOL CDlgFeature::OnInitDialog() 
{
  UINT8      i;
  int        ix;
  FGPINFO    Info;
  CComboBox *pCombo;
  char       Name[128];
  char       Title[256];
  RECT       ParentRect,MyRect;
  CWnd      *pParent;
  UCHAR      ParameterList[] =
  {
    FGP_BRIGHTNESS,
    FGP_SHARPNESS,
    FGP_WHITEBALCB,
    FGP_WHITEBALCR,
    FGP_HUE,
    FGP_SATURATION,
    FGP_GAMMA,
    FGP_SHUTTER,
    FGP_GAIN,
    FGP_IRIS,
    FGP_FOCUS,
    FGP_ZOOM,
    FGP_PAN,
    FGP_TILT,
    FGP_OPTICALFILTER
  };

  CDialog::OnInitDialog();

  // Put window to lower left side of our parent window
  pParent=GetParent();
  if(pParent)
  {
    pParent->GetWindowRect(&ParentRect);
    GetWindowRect(&MyRect);
    MoveWindow(ParentRect.left,ParentRect.bottom-(MyRect.bottom-MyRect.top)-
               GetSystemMetrics(SM_CYMENUSIZE),
               MyRect.right-MyRect.left,MyRect.bottom-MyRect.top);
  }

  // Set window title
  m_pCamera->GetDeviceName(Name,sizeof(Name));
  wsprintf(Title,"Parameters for %s",Name);
  SetWindowText(Title);

  // Add all parameters
  pCombo=(CComboBox*)GetDlgItem(IDC_PARAMETER);
  for(i=0;i<sizeof(ParameterList);i++)
  {
    if(FCE_NOERROR==m_pCamera->GetParameterInfo(ParameterList[i],&Info))
    {
      ix=pCombo->AddString(FGParameterStr[ParameterList[i]]);
      pCombo->SetItemData(ix,ParameterList[i]);
    }
  }

  // Select feature 0
  pCombo->SetCurSel(0);
  SelectParameter(0);

  // Start a timer to update values automatically
  SetTimer((UINT_PTR)m_hWnd,300,NULL);

  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// Select a specific feature by its list index.

void CDlgFeature::SelectParameter(int i)
{
  CComboBox   *pCombo;
  CSliderCtrl *pSlider;
  char         cbuf[16];
  FGPSFEATURE *pSpec;

  pCombo=(CComboBox*)GetDlgItem(IDC_PARAMETER);
  pCombo->SetCurSel(i);
  m_Ix=(UINT32)pCombo->GetItemData(i);

  if(FCE_NOERROR!=m_pCamera->GetParameterInfo((UINT16)m_Ix,&m_Info))
  {
    GetDlgItem(IDS_VALUE)->EnableWindow(FALSE);
    GetDlgItem(IDB_ON)->EnableWindow(FALSE);
    GetDlgItem(IDB_AUTO)->EnableWindow(FALSE);
    GetDlgItem(IDB_ONESHOT)->EnableWindow(FALSE);
  }
  else
  {
    pSpec=&m_Info.Specific.Data.FeatureInfo;
    GetDlgItem(IDB_ON)->EnableWindow(pSpec->OnOffCap?TRUE:FALSE);
    ((CButton*)GetDlgItem(IDB_ON))->SetCheck(pSpec->OnOffState?1:0);
    
    GetDlgItem(IDB_AUTO)->EnableWindow(pSpec->AutoCap?TRUE:FALSE);
    if(pSpec->AutoState)
    {
      ((CButton*)GetDlgItem(IDB_AUTO))->SetCheck(1);
      GetDlgItem(IDS_VALUE)->EnableWindow(FALSE);
    }
    else
    {
      ((CButton*)GetDlgItem(IDB_AUTO))->SetCheck(0);
      GetDlgItem(IDS_VALUE)->EnableWindow(TRUE);
    }

    GetDlgItem(IDB_ONESHOT)->EnableWindow(pSpec->OnePushCap?TRUE:FALSE);

    pSlider=(CSliderCtrl*)GetDlgItem(IDS_VALUE);
    if(m_Info.MinValue>=m_Info.MaxValue)
    {
      pSlider->SetRange(0,1,TRUE);
      pSlider->SetTicFreq(1);
      pSlider->EnableWindow(FALSE);
    }
    else
    {
      pSlider->SetRange(m_Info.MinValue,m_Info.MaxValue,TRUE);
      if((m_Info.MaxValue-m_Info.MinValue)>20)
       pSlider->SetTicFreq((m_Info.MaxValue-m_Info.MinValue+1)/4);
      else
       pSlider->SetTicFreq(1);
      pSlider->SetPos(m_Info.IsValue);
    }

    wsprintf(cbuf,"%d",m_Info.IsValue);
    GetDlgItem(IDE_VALUE)->SetWindowText(cbuf);
  }
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on slider movement.

void CDlgFeature::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
  UINT32  Value;
  CSliderCtrl *pSlider;
  char    cbuf[16];
  
  //CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  pSlider=(CSliderCtrl*)pScrollBar;

  Value=m_Info.IsValue;
  switch(nSBCode)
  {
    case TB_TOP:
     Value=m_Info.MinValue;
     break;

    case TB_BOTTOM:
     Value=m_Info.MaxValue;
     break;

    case TB_LINEDOWN:
     if(Value<m_Info.MaxValue)
      Value++;
     break;

    case TB_LINEUP:
     if(Value>m_Info.MinValue)
      Value--;
     break;

    case TB_PAGEUP:
     if(Value>m_Info.MinValue+PAGESIZE)
      Value-=PAGESIZE;
     else
      Value=m_Info.MinValue;
     pSlider->SetPos(Value);
     break;

    case TB_PAGEDOWN:
     if(Value<m_Info.MaxValue-PAGESIZE)
      Value+=PAGESIZE;
     else
      Value=m_Info.MaxValue;
     pSlider->SetPos(Value);
     break;

    case TB_THUMBTRACK:
     Value=(UINT16)nPos;
     break;
  }

  wsprintf(cbuf,"%d",Value);
  GetDlgItem(IDE_VALUE)->SetWindowText(cbuf);

  if(FCE_NOERROR==m_pCamera->SetParameter((UINT16)m_Ix,Value))
  {
    m_Info.IsValue=Value;
    pSlider->SetPos(m_Info.IsValue);
  }
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on selection change in parameter box.

void CDlgFeature::OnSelchangeParameter() 
{
  SelectParameter(((CComboBox*)GetDlgItem(IDC_PARAMETER))->GetCurSel());
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on button On/Off.

void CDlgFeature::OnFeatureOn() 
{
  int NewState;

  NewState=((CButton*)GetDlgItem(IDB_ON))->GetCheck()?1:0;
  if(NewState==0)
  {
    m_pCamera->SetParameter((UINT16)m_Ix,PVAL_OFF);
  }
  else
  {
    m_pCamera->GetParameterInfo((UINT16)m_Ix,&m_Info);
    m_pCamera->SetParameter((UINT16)m_Ix,m_Info.IsValue);
  }
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on feature button.

void CDlgFeature::OnFeatureAuto() 
{
  int NewState;

  NewState=((CButton*)GetDlgItem(IDB_AUTO))->GetCheck()?1:0;
  if(NewState==1)
  {
    m_pCamera->SetParameter((UINT16)m_Ix,PVAL_AUTO);
    GetDlgItem(IDS_VALUE)->EnableWindow(FALSE);
  }
  else
  {
    m_pCamera->GetParameterInfo((UINT16)m_Ix,&m_Info);
    m_pCamera->SetParameter((UINT16)m_Ix,m_Info.IsValue);
    GetDlgItem(IDS_VALUE)->EnableWindow(TRUE);
  }
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on feature button.

void CDlgFeature::OnFeatureOneshot() 
{
  int NewState;

  NewState=((CButton*)GetDlgItem(IDB_ONESHOT))->GetCheck()?1:0;
  if(NewState==1)
  {
    m_pCamera->SetParameter((UINT16)m_Ix,PVAL_ONESHOT);
  }
  else
  {
    m_pCamera->GetParameterInfo((UINT16)m_Ix,&m_Info);
    m_pCamera->SetParameter((UINT16)m_Ix,m_Info.IsValue);
  }
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on timer. We always update the value of the feature.

void CDlgFeature::OnTimer(UINT_PTR nIDEvent) 
{
  CSliderCtrl *pSlider;
  char         cbuf[16];

  m_pCamera->GetParameterInfo((UINT16)m_Ix,&m_Info);

  // Set ON/OFF
  if(m_Info.Specific.Data.FeatureInfo.OnOffCap)
   ((CButton*)GetDlgItem(IDB_ON))->SetCheck(m_Info.Specific.Data.FeatureInfo.OnOffState);

  // Set One Shot button
  if(m_Info.Specific.Data.FeatureInfo.OnePushCap)
   ((CButton*)GetDlgItem(IDB_ONESHOT))->SetCheck(m_Info.Specific.Data.FeatureInfo.OnePushState);

  // Set Auto
  if(m_Info.Specific.Data.FeatureInfo.AutoCap)
  {
    if(m_Info.Specific.Data.FeatureInfo.AutoState)
    {
      ((CButton*)GetDlgItem(IDB_AUTO))->SetCheck(1);
      GetDlgItem(IDS_VALUE)->EnableWindow(FALSE);
    }
    else
    {
      ((CButton*)GetDlgItem(IDB_AUTO))->SetCheck(0);
      GetDlgItem(IDS_VALUE)->EnableWindow(TRUE);
    }
  }

  // If we can read this feature we update our dialog
  if(m_Info.Specific.Data.FeatureInfo.ReadOutCap)
  {
    // Set slider
    pSlider=(CSliderCtrl*)GetDlgItem(IDS_VALUE);
    pSlider->SetPos(m_Info.IsValue);

    // Set decimal value 
    wsprintf(cbuf,"%d",m_Info.IsValue);
    GetDlgItem(IDE_VALUE)->SetWindowText(cbuf);
  }

  CDialog::OnTimer(nIDEvent);
}

/////////////////////////////////////////////////////////////////////////////
// Reaction on close request. We stop our timer.

void CDlgFeature::OnClose() 
{
  // We must kill the timer, otherwise Windows might crash
  KillTimer((UINT_PTR)m_hWnd);
  CDialog::OnClose();
}

/////////////////////////////////////////////////////////////////////////////
// Delete ourself...

void CDlgFeature::PostNcDestroy() 
{
  CDialog::PostNcDestroy();
  delete this;
}
